package com.nfu.zlsp.dev.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.nfu.zlsp.dev.common.utils.PageUtils;
import com.nfu.zlsp.dev.common.utils.Query;
import com.nfu.zlsp.dev.dao.AddressDao;
import com.nfu.zlsp.dev.entity.AddressEntity;
import com.nfu.zlsp.dev.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;


@Service("addressService")
public class AddressServiceImpl extends ServiceImpl<AddressDao, AddressEntity> implements AddressService {

    @Autowired
    private AddressDao addressDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AddressEntity> page = this.page(
                new Query<AddressEntity>().getPage(params),
                new QueryWrapper<AddressEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    public List<AddressEntity> getRoot() {
        return addressDao.selectRoot();
    }

    @Override
    public List<AddressEntity> getList(String addressName, String addressRegionid) {
        return addressDao.select(addressName,addressRegionid);
    }

    /**
     * 通过区级编号 获取完整地址
     * @param pickupAddress
     * @return
     */
    @Override
    public String getAddressByRegionId(String pickupAddress) {
        AddressEntity district = addressDao.selectOne(new QueryWrapper<AddressEntity>().eq("address_areaId", pickupAddress));
        if (district != null) {
            AddressEntity city = addressDao.selectOne(new QueryWrapper<AddressEntity>().eq("address_areaId", district.getAddressRegionid()));
            if (city != null) {
                AddressEntity province = addressDao.selectOne(new QueryWrapper<AddressEntity>().eq("address_areaId", city.getAddressRegionid()));
                return province.getAddressName() + city.getAddressName() + district.getAddressName();
            }
        }
        return null;
    }

}
















